﻿using Barcode_Project.Entity;
using Barcode_Project.Local;
using Barcode_Project.Network;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Barcode_Project.InitPrint
{
    public partial class ProductSelector : DevExpress.XtraEditors.XtraForm
    {
        public FilteredProduct selectedProduct { get; set; }

        public Dictionary<string, string> selectedPro = new Dictionary<string, string>();

        public ProductSelector(string spec = "")
        {
            InitializeComponent();

            input_spec.Text = spec;

            SearchProduct();

        }
        // 搜索存货
        void SearchProduct()
        {
            var dic = new Dictionary<string, string>();
            CommonMethods.SetParameterValue(ref dic, group_data.Controls, "input_");

            var data = new RemoteRequest().SearchInventories(dic);

            gridControl_products.DataSource = data;
            gridControl_products.RefreshDataSource();
        }

        private void button_search_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SearchProduct();
        }

        private void button_confirm_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;

            var gridView = gridControl_products.FocusedView as GridView;

            var item = (ProductSelectItem)gridView_products.GetRow(gridView.FocusedRowHandle);

            selectedProduct = new FilteredProduct
            {
                proCode = item.proCode,
                proName = item.proName,
                spec = item.spec,
                proUnit = item.proUnit,
            };

            selectedPro["proCode"] = selectedProduct.proCode;
            selectedPro["proName"] = selectedProduct.proName;
            selectedPro["spec"] = selectedProduct.spec;
            selectedPro["proUnit"] = selectedProduct.proUnit;

            Close();
        }

        private void input_proCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SearchProduct();
            }
        }

        private void input_proName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SearchProduct();
            }
        }

        private void input_spec_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SearchProduct();
            }
        }

        private void gridView_products_DoubleClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;

            var gridView = gridControl_products.FocusedView as GridView;
            var item = (ProductSelectItem)gridView_products.GetRow(gridView.FocusedRowHandle);

            selectedProduct = new FilteredProduct
            {
                proCode = item.proCode,
                proName = item.proName,
                spec = item.spec,
                proUnit = item.proUnit,
            };

            selectedPro["proCode"] = selectedProduct.proCode;
            selectedPro["proName"] = selectedProduct.proName;
            selectedPro["spec"] = selectedProduct.spec;
            selectedPro["proUnit"] = selectedProduct.proUnit;

            Close();
        }

        private void btn_close_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }
    }
}